const Flatten = require("../template")

function testFlattenComplexScenarios() {
  console.log(
    "1. 复杂扁平化系统测试:",
    (() => {
      const arr = [
        { id: 1, items: [1, 2, 3] },
        [{ id: 2, items: [4, 5] }, { id: 3, items: [6, 7, 8] }],
        { id: 4, items: [9, 10] }
      ]
      
      // 复杂扁平化操作
      const flattened = Flatten.flattenWithMap(arr, (item) => ({
        ...item,
        processed: true,
        items: item.items.map(i => i * 2)
      }), 1)
      
      return flattened.length === 3 &&
             flattened.every(item => item.processed === true) &&
             flattened[0].items.length === 3
    })()
  )

  console.log(
    "2. 扁平化动态负载测试:",
    (() => {
      const arr = Array.from({ length: 100 }, (_, i) => [
        i, [i + 1, i + 2], [i + 3, i + 4]
      ])
      
      // 动态负载测试
      const loadPatterns = [
        { method: 'flatten', depth: 1 },
        { method: 'flattenDeep', depth: Infinity },
        { method: 'flattenDepth', depth: 2 },
        { method: 'flattenWithFilter', depth: 1, filter: (item) => item % 2 === 0 },
        { method: 'flattenWithMap', depth: 1, map: (item) => item * 2 }
      ]
      
      let successCount = 0
      loadPatterns.forEach(pattern => {
        try {
          let result
          if (pattern.method === 'flatten') {
            result = Flatten.flatten(arr)
          } else if (pattern.method === 'flattenDeep') {
            result = Flatten.flattenDeep(arr)
          } else if (pattern.method === 'flattenDepth') {
            result = Flatten.flattenDepth(arr, pattern.depth)
          } else if (pattern.method === 'flattenWithFilter') {
            result = Flatten.flattenWithFilter(arr, pattern.filter, pattern.depth)
          } else if (pattern.method === 'flattenWithMap') {
            result = Flatten.flattenWithMap(arr, pattern.map, pattern.depth)
          }
          
          if (result && result.length > 0) {
            successCount++
          }
        } catch (error) {
          // 某些操作可能失败
        }
      })
      
      return successCount >= 4
    })()
  )

  console.log(
    "3. 扁平化边界条件测试:",
    (() => {
      const boundaryTests = [
        () => Flatten.flatten([]), // 空数组
        () => Flatten.flatten([1]), // 单元素
        () => Flatten.flatten([[]]), // 空子数组
        () => Flatten.flatten([[[]]]), // 嵌套空数组
        () => Flatten.flatten([null]), // null元素
        () => Flatten.flatten([undefined]), // undefined元素
        () => Flatten.flatten([1, null, [2, 3]]), // 混合类型
        () => Flatten.flatten([1, [2, [3, [4, 5]]]]), // 深度嵌套
        () => Flatten.flatten([1, [2, 3], [4, [5, 6]]]), // 不规则嵌套
        () => Flatten.flatten([1, [2, 3], 4, [5, 6]]), // 混合嵌套
        () => Flatten.flatten([1, [2, 3], [4, 5], 6]), // 混合嵌套
        () => Flatten.flatten([1, [2, 3], [4, 5], [6, 7]]) // 规则嵌套
      ]
      
      let successCount = 0
      boundaryTests.forEach(test => {
        try {
          const result = test()
          if (result !== undefined) {
            successCount++
          }
        } catch (error) {
          // 某些操作可能失败
        }
      })
      
      return successCount >= 8 // 至少8个操作成功
    })()
  )

  console.log(
    "4. 扁平化错误恢复测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5]]
      
      try {
        // 错误恢复测试
        const processor = async (item) => {
          if (item === 3) {
            throw new Error('Test error')
          }
          return item * 2
        }
        
        return new Promise((resolve) => {
          Flatten.flattenAsync(arr, processor, 1).then((result) => {
            resolve(result.length > 0)
          }).catch(() => {
            resolve(false)
          })
        })
      } catch (error) {
        return false
      }
    })()
  )

  console.log(
    "5. 扁平化内存管理测试:",
    (() => {
      const arr = Array.from({ length: 1000 }, (_, i) => [
        i, [i + 1, i + 2], [i + 3, i + 4]
      ])
      
      // 内存管理测试
      const initialMemory = process.memoryUsage().heapUsed
      
      // 执行大量扁平化操作
      for (let i = 0; i < 100; i++) {
        Flatten.flatten(arr)
        Flatten.flattenDeep(arr)
        Flatten.flattenDepth(arr, 2)
      }
      
      const finalMemory = process.memoryUsage().heapUsed
      const memoryIncrease = finalMemory - initialMemory
      
      return memoryIncrease < 100 * 1024 * 1024 // 100MB以内
    })()
  )

  console.log(
    "6. 扁平化并发操作测试:",
    (() => {
      const arr = Array.from({ length: 100 }, (_, i) => [
        i, [i + 1, i + 2], [i + 3, i + 4]
      ])
      
      // 并发操作测试
      const operations = [
        () => Flatten.flatten(arr),
        () => Flatten.flattenDeep(arr),
        () => Flatten.flattenDepth(arr, 2),
        () => Flatten.flattenWithFilter(arr, (item) => item % 2 === 0, 1),
        () => Flatten.flattenWithMap(arr, (item) => item * 2, 1),
        () => Flatten.flattenWithUnique(arr, 1)
      ]
      
      const results = operations.map(op => op())
      
      return results.every(result => result && result.length > 0)
    })()
  )

  console.log(
    "7. 扁平化复杂查询测试:",
    (() => {
      const arr = [
        { id: 1, items: [1, 2, 3] },
        [{ id: 2, items: [4, 5] }, { id: 3, items: [6, 7, 8] }],
        { id: 4, items: [9, 10] }
      ]
      
      // 复杂查询测试
      const queries = [
        { method: 'flatten', depth: 1 },
        { method: 'flattenWithFilter', depth: 1, filter: (item) => item.id > 2 },
        { method: 'flattenWithMap', depth: 1, map: (item) => ({ ...item, processed: true }) },
        { method: 'flattenWithGroup', depth: 1, group: (item) => item.id % 2 === 0 ? 'even' : 'odd' },
        { method: 'flattenWithStats', depth: 1 }
      ]
      
      const results = queries.map(query => {
        let result
        if (query.method === 'flatten') {
          result = Flatten.flatten(arr)
        } else if (query.method === 'flattenWithFilter') {
          result = Flatten.flattenWithFilter(arr, query.filter, query.depth)
        } else if (query.method === 'flattenWithMap') {
          result = Flatten.flattenWithMap(arr, query.map, query.depth)
        } else if (query.method === 'flattenWithGroup') {
          result = Flatten.flattenWithGroup(arr, query.group, query.depth)
        } else if (query.method === 'flattenWithStats') {
          result = Flatten.flattenWithStats(arr, query.depth)
        }
        return result && (Array.isArray(result) ? result.length > 0 : Object.keys(result).length > 0)
      })
      
      return results.every(result => result === true)
    })()
  )

  console.log(
    "8. 扁平化状态一致性测试:",
    (() => {
      const arr = [1, [2, 3], [4, 5]]
      
      // 状态一致性测试
      const operations = [
        () => Flatten.flatten(arr),
        () => Flatten.flattenDeep(arr),
        () => Flatten.flattenDepth(arr, 2),
        () => Flatten.flattenWithMap(arr, (item) => item * 2, 1),
        () => Flatten.flattenWithFilter(arr, (item) => item % 2 === 0, 1)
      ]
      
      const results = operations.map(op => op())
      
      return results.every(result => result && result.length > 0) &&
             results[0].length === 5 && // flatten结果
             results[1].length === 5 && // flattenDeep结果
             results[2].length === 5 && // flattenDepth结果
             results[3].length === 5 && // flattenWithMap结果
             results[4].length === 2 // flattenWithFilter结果
    })()
  )

  console.log(
    "9. this上下文复杂场景测试:",
    (() => {
      const complexObj = {
        multiplier: 3,
        processComplex: function(arr) {
          const flattened = Flatten.flattenWithMap(arr, (item) => item * this.multiplier, 1)
          return flattened.length
        }
      }
      
      const arr = [1, [2, 3], [4, 5]]
      return complexObj.processComplex(arr) === 5
    })()
  )

  console.log(
    "10. 复杂对象this绑定复杂场景测试:",
    (() => {
      const complexConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(arr) {
          const flattened = Flatten.flattenWithFilter(arr, (item) => item >= this.config.threshold, 1)
          return flattened.length > 0
        }
      }
      
      const arr = [1, [2, 3], [4, 5]]
      return complexConfigObj.processWithConfig(arr) === true
    })()
  )

  console.log(
    "11. 扁平化复杂操作序列测试:",
    (() => {
      const arr = Array.from({ length: 100 }, (_, i) => [
        i, [i + 1, i + 2], [i + 3, i + 4]
      ])
      
      // 复杂操作序列
      const operationSequence = [
        { method: 'flatten', depth: 1 },
        { method: 'flattenWithMap', depth: 1, map: (item) => item * 2 },
        { method: 'flattenWithFilter', depth: 1, filter: (item) => item % 4 === 0 },
        { method: 'flattenWithSort', depth: 1, sort: (a, b) => a - b },
        { method: 'flattenWithUnique', depth: 1 }
      ]
      
      let successCount = 0
      operationSequence.forEach(seq => {
        try {
          let result
          if (seq.method === 'flatten') {
            result = Flatten.flatten(arr)
          } else if (seq.method === 'flattenWithMap') {
            result = Flatten.flattenWithMap(arr, seq.map, seq.depth)
          } else if (seq.method === 'flattenWithFilter') {
            result = Flatten.flattenWithFilter(arr, seq.filter, seq.depth)
          } else if (seq.method === 'flattenWithSort') {
            result = Flatten.flattenWithSort(arr, seq.sort, seq.depth)
          } else if (seq.method === 'flattenWithUnique') {
            result = Flatten.flattenWithUnique(arr, seq.depth)
          }
          
          if (result && result.length > 0) {
            successCount++
          }
        } catch (error) {
          // 某些操作可能失败
        }
      })
      
      return successCount >= 4 // 至少4个操作成功
    })()
  )

  console.log(
    "12. 扁平化复杂场景综合测试:",
    (() => {
      const arr = [
        { id: 1, items: [1, 2, 3] },
        [{ id: 2, items: [4, 5] }, { id: 3, items: [6, 7, 8] }],
        { id: 4, items: [9, 10] }
      ]
      
      // 综合复杂场景
      const scenarios = [
        // 场景1: 基本扁平化
        () => {
          const flattened = Flatten.flatten(arr)
          return flattened.length === 3
        },
        // 场景2: 深度扁平化
        () => {
          const flattened = Flatten.flattenDeep(arr)
          return flattened.length === 3
        },
        // 场景3: 带映射扁平化
        () => {
          const flattened = Flatten.flattenWithMap(arr, (item) => ({
            ...item,
            processed: true
          }), 1)
          return flattened.length === 3 && flattened[0].processed === true
        },
        // 场景4: 带过滤扁平化
        () => {
          const flattened = Flatten.flattenWithFilter(arr, (item) => item.id > 2, 1)
          return flattened.length > 0
        },
        // 场景5: 最终验证
        () => {
          const flattened = Flatten.flatten(arr)
          return flattened.length === 3 && flattened[0].id === 1
        }
      ]
      
      const results = scenarios.map(scenario => scenario())
      
      return results.every(result => result === true)
    })()
  )
}

testFlattenComplexScenarios()
